package com.xy.demo.excel.converter;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ReadConverterContext;
import com.alibaba.excel.converters.WriteConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum;

import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.CellData;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.xy.demo.Util.Util;
import org.apache.poi.ss.usermodel.DateUtil;

import java.util.Calendar;
import java.util.Date;

public class LongDateConverter implements Converter<Long> {
    @Override
    public Class<?> supportJavaTypeKey() {
        return Date.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.NUMBER;
    }


    @Override
    public Long convertToJavaData(ReadConverterContext<?> context) throws Exception {
        //CellData转对象属性
        String cellStr = context.getReadCellData().getStringValue();
        if (Util.isEmpty(cellStr)) return null;
        return Util.convertTime(Util.second());
    }

    @Override
    public WriteCellData<?> convertToExcelData(WriteConverterContext<Long> context) throws Exception {
        //对象属性转CellData
        Long cellValue = context.getValue();
        if (cellValue == null) {
            return new WriteCellData<>("");
        }
        return new WriteCellData<>(Util.formatDate(cellValue,"YYYY-MM-dd HH:mm:ss"));
    }







}
